package com.raspberryCar.model;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.CommandArgumentParser;

public class SensorCheck implements Runnable {
	private Car car;
	final GpioPinDigitalInput rightInput;
	final GpioPinDigitalInput leftInput;
	final GpioPinDigitalInput fawInput;
	final GpioPinDigitalInput backInput;

	public SensorCheck(GpioController gpio, Car car) {
		this.car = car;
		// by default we will use gpio pin #01; however, if an argument
		// has been provided, then lookup the pin by address

		// by default we will use gpio pin PULL-UP; however, if an argument
		// has been provided, then use the specified pull resistance
		PinPullResistance pull = CommandArgumentParser
				.getPinPullResistance(PinPullResistance.PULL_UP // default pin
				); // argument array to search in

		// provision gpio pin as an input pin
		rightInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, pull);
		leftInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, pull);
		fawInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, pull);
		backInput = gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, pull);
	}

	@Override
	public void run() {
		while (true) {
			if (rightInput.getState().isLow()) {
				System.out.println("1");
			}
			if (leftInput.getState().isLow()) {
				System.out.println("2");
			}
			if (fawInput.getState().isLow()) {
				System.out.println("3");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (backInput.getState().isLow()) {
				System.out.println("4");
				car.stop();
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

		// gpio.shutdown();
	}

}
